package com.cnlbc.Dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.cnlbc.Model.User;

public class UserDao {
	/**
	 * 登入验证
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public User login(Connection con,User user) throws Exception {
		User resultuser=null;
		String sql="select * from user where username=? and password=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUsername());
		pstmt.setString(2, user.getPassword());
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()) {
			resultuser=new User();
			resultuser.setId(rs.getInt("id"));
			resultuser.setUsername(rs.getString("username"));
			resultuser.setPassword(rs.getString("password"));
		}
		return resultuser;
	}
	/**
	 * 注册账号
	 * @param con
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public int Register(Connection con,User user) throws Exception {
		String sql="insert into user values(null,?,?)";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, user.getUsername());
		pstmt.setString(2, user.getPassword());
		return pstmt.executeUpdate();
	}
	/**
	 * 用户名是否存在
	 * @param con
	 * @param name
	 * @return
	 * @throws Exception
	 */
	public boolean isExist(Connection con,String name) throws Exception {
		String sql="select id from user where userName=?";
		PreparedStatement pstmt=con.prepareStatement(sql);
		pstmt.setString(1, name);
		ResultSet rs=pstmt.executeQuery();
		return rs.next();
	}
}
